package serveur;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.UnknownHostException;
import java.rmi.Naming;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;

/**
 * 
 * @author KaHvEcI
 *
 * @param <E>
 */


public class ServerRMI <E> {
	
    private String nom;
    private String addresseIp;
    private int port;
    
    
	public ServerRMI(String nomS) throws UnknownHostException{
        
        nom = nomS;
        port = 1099;
        // Recuperation de l'adresse de la machine serveur
        InetAddress ServeurIp = InetAddress.getLocalHost();
        addresseIp=ServeurIp.getHostAddress();
        
    }
    
	
	public ServerRMI(String nomS, int p, String ip){
		
		nom=nomS;
		port=p;
		addresseIp=ip;
		
	}
	
    @SuppressWarnings("unchecked")
	public void start(Remote r) throws RemoteException, MalformedURLException{
		
    	E e = (E) r;
    	LocateRegistry.createRegistry(port);
    	String url = "rmi://"+addresseIp+"/"+nom;
    	Naming.rebind(url,(Remote) e);

	}
    
    public String getIp(){
    	
    	return addresseIp;
    }
    
    public int getPort(){
    	
    	return port;
    }
    
    public String getNom(){
    	
    	return nom;
    }
}